home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap10 / PoePoem / PoePoem.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  5.8 KB  |  178 lines

  1. /*-------------------------------------------
  2.    POEPOEM.C -- Demonstrates Custom Resource
  3.                 (c) Charles Petzold, 1998
  4.   -------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. HINSTANCE hInst ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15. {
  16.      TCHAR    szAppName [16], szCaption [64], szErrMsg [64] ;
  17.      HWND     hwnd ;
  18.      MSG      msg ;
  19.      WNDCLASS wndclass ;
  20.      
  21.      LoadString (hInstance, IDS_APPNAME, szAppName, 
  22.                             sizeof (szAppName) / sizeof (TCHAR)) ;
  23.  
  24.      LoadString (hInstance, IDS_CAPTION, szCaption, 
  25.                             sizeof (szCaption) / sizeof (TCHAR)) ;
  26.         
  27.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  28.      wndclass.lpfnWndProc   = WndProc ;
  29.      wndclass.cbClsExtra    = 0 ;
  30.      wndclass.cbWndExtra    = 0 ;
  31.      wndclass.hInstance     = hInstance ;
  32.      wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
  33.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  34.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  35.      wndclass.lpszMenuName  = NULL ;
  36.      wndclass.lpszClassName = szAppName ;
  37.      
  38.      if (!RegisterClass (&wndclass))
  39.      {
  40.           LoadStringA (hInstance, IDS_APPNAME, (char *) szAppName, 
  41.                                   sizeof (szAppName)) ;
  42.  
  43.           LoadStringA (hInstance, IDS_ERRMSG, (char *) szErrMsg, 
  44.                                    sizeof (szErrMsg)) ;
  45.  
  46.           MessageBoxA (NULL, (char *) szErrMsg, 
  47.                              (char *) szAppName, MB_ICONERROR) ;
  48.           return 0 ;
  49.      }
  50.      
  51.      hwnd = CreateWindow (szAppName, szCaption,
  52.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  53.                           CW_USEDEFAULT, CW_USEDEFAULT,
  54.                           CW_USEDEFAULT, CW_USEDEFAULT,
  55.                           NULL, NULL, hInstance, NULL) ;
  56.      
  57.      ShowWindow (hwnd, iCmdShow) ;
  58.      UpdateWindow (hwnd) ;
  59.      
  60.      while (GetMessage (&msg, NULL, 0, 0))
  61.      {
  62.           TranslateMessage (&msg) ;
  63.           DispatchMessage (&msg) ;
  64.      }
  65.      return msg.wParam ;
  66. }
  67.  
  68. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  69. {
  70.      static char  * pText ;
  71.      static HGLOBAL hResource ;
  72.      static HWND    hScroll ;
  73.      static int     iPosition, cxChar, cyChar, cyClient, iNumLines, xScroll ;
  74.      HDC            hdc ;
  75.      PAINTSTRUCT    ps ;
  76.      RECT           rect ;
  77.      TEXTMETRIC     tm ;
  78.  
  79.      switch (message)
  80.      {
  81.      case WM_CREATE :
  82.           hdc = GetDC (hwnd) ;
  83.           GetTextMetrics (hdc, &tm) ;
  84.           cxChar = tm.tmAveCharWidth ;
  85.           cyChar = tm.tmHeight + tm.tmExternalLeading ;
  86.           ReleaseDC (hwnd, hdc) ;
  87.           
  88.           xScroll = GetSystemMetrics (SM_CXVSCROLL) ;
  89.           
  90.           hScroll = CreateWindow (TEXT ("scrollbar"), NULL,
  91.                                   WS_CHILD | WS_VISIBLE | SBS_VERT,
  92.                                   0, 0, 0, 0,
  93.                                   hwnd, (HMENU) 1, hInst, NULL) ;
  94.           
  95.           hResource = LoadResource (hInst, 
  96.                       FindResource (hInst, TEXT ("AnnabelLee"),
  97.                                            TEXT ("TEXT"))) ;
  98.           
  99.           pText = (char *) LockResource (hResource) ;
  100.           iNumLines = 0 ;
  101.           
  102.           while (*pText != '\\' && *pText != '\0')
  103.           {
  104.                if (*pText == '\n')
  105.                     iNumLines ++ ;
  106.                pText = AnsiNext (pText) ;
  107.           }
  108.           *pText = '\0' ;
  109.           
  110.           SetScrollRange (hScroll, SB_CTL, 0, iNumLines, FALSE) ;
  111.           SetScrollPos   (hScroll, SB_CTL, 0, FALSE) ;
  112.           return 0 ;
  113.           
  114.      case WM_SIZE :
  115.           MoveWindow (hScroll, LOWORD (lParam) - xScroll, 0,
  116.                       xScroll, cyClient = HIWORD (lParam), TRUE) ;
  117.           SetFocus (hwnd) ;
  118.           return 0 ;
  119.           
  120.      case WM_SETFOCUS :
  121.           SetFocus (hScroll) ;
  122.           return 0 ;
  123.           
  124.      case WM_VSCROLL :
  125.           switch (wParam)
  126.           {
  127.           case SB_TOP :
  128.                iPosition = 0 ;
  129.                break ;
  130.           case SB_BOTTOM :
  131.                iPosition = iNumLines ;
  132.                break ;
  133.           case SB_LINEUP :
  134.                iPosition -= 1 ;
  135.                break ;
  136.           case SB_LINEDOWN :
  137.                iPosition += 1 ;
  138.                break ;
  139.           case SB_PAGEUP :
  140.                iPosition -= cyClient / cyChar ;
  141.                break ;
  142.           case SB_PAGEDOWN :
  143.                iPosition += cyClient / cyChar ;
  144.                break ;
  145.           case SB_THUMBPOSITION :
  146.                iPosition = LOWORD (lParam) ;
  147.                break ;
  148.           }
  149.           iPosition = max (0, min (iPosition, iNumLines)) ;
  150.           
  151.           if (iPosition != GetScrollPos (hScroll, SB_CTL))
  152.           {
  153.                SetScrollPos (hScroll, SB_CTL, iPosition, TRUE) ;
  154.                InvalidateRect (hwnd, NULL, TRUE) ;
  155.           }
  156.           return 0 ;
  157.           
  158.      case WM_PAINT :
  159.           hdc = BeginPaint (hwnd, &ps) ;
  160.                
  161.           pText = (char *) LockResource (hResource) ;
  162.                
  163.           GetClientRect (hwnd, &rect) ;
  164.           rect.left += cxChar ;
  165.           rect.top  += cyChar * (1 - iPosition) ;
  166.           DrawTextA (hdc, pText, -1, &rect, DT_EXTERNALLEADING) ;
  167.  
  168.           EndPaint (hwnd, &ps) ;
  169.           return 0 ;
  170.                
  171.      case WM_DESTROY :
  172.           FreeResource (hResource) ;
  173.           PostQuitMessage (0) ;
  174.           return 0 ;
  175.      }
  176.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  177. }
  178.